//
//  ThemeColor.m
//  微blog
//
//  Created by Jorn.Wu on 15/8/24.
//  Copyright (c) 2015年 Jorn.Wu(jorn_wza@sina.com). All rights reserved.
//

#import "ThemeUtils.h"
#import "ThemeManager.h"

@interface ThemeUtils ()

@property (nonatomic, copy) NSString *colorName;
@property (nonatomic, strong) UIColor *currentColor;

@property (nonatomic, copy) NSString *imageName;
@property (nonatomic, strong) UIImage *currentImage;

@end

@implementation ThemeUtils

/**
 * 重写dealloc方法，旨在移除观察者
 */
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeDidChangedNotification object:nil];
}


/**
 * 重写init方法，旨在让该类可以响应切换主题通知
 */
- (instancetype)init{
    if (self = [super init]) {
        /**
         * 注册成为接收者
         */
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChangedNotificationAction:) name:kThemeDidChangedNotification object:nil];
    }
    
    return self;
}

/**
 * 通知的响应方法
 */
- (void)themeDidChangedNotificationAction:(NSNotification *)noti{
    [self reloadColor];
    [self reloadImage];
}


///----------------------------Color--------------------------------

/**
 * 重新加载颜色
 */
- (void)reloadColor{
    ThemeManager *manager = [ThemeManager defualtManager];
    
    /**
     * 通过主题管理对象manager来获取颜色
     */
    UIColor *color = [manager getThemeColor:self.colorName];
    
    self.currentColor = color;
}


/**
 * 重写set方法，旨在更改颜色
 */
- (void)setColorName:(NSString *)colorName{
    if (![_colorName isEqualToString:colorName]) {
        _colorName = [colorName copy];
        [self reloadColor];
    }
}



+ (UIColor *)themeColorWithName:(NSString *)name{
    
    ThemeUtils *utils = [[self alloc] init];
    utils.colorName = [name copy];
    return utils.currentColor;
    
}



///----------------------------Image--------------------------------


/**
 * 重新加载图片
 */
- (void)reloadImage{
    ThemeManager *manager = [ThemeManager defualtManager];
    
    /**
     * 通过主题管理对象manager来获取图片
     */
    UIImage *image = [manager getThemeImage:self.imageName];
    
    self.currentImage = image;
}


/**
 * 重写set方法，旨在更改颜色
 */
- (void)setImageName:(NSString *)imageName{
    if (![_imageName isEqualToString:imageName]) {
        _imageName = [imageName copy];
        [self reloadImage];
    }
}



+ (UIImage *)themeImageWithName:(NSString *)name{
    
    ThemeUtils *utils = [[self alloc] init];
    utils.imageName = [name copy];
    return utils.currentImage;
    
}


@end
